package com.ruoyi.common.utils.erp;

import com.ruoyi.common.core.domain.AjaxResult;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author YanWenliang
 * @date 2024/11/9 15:43
 * @desciption: 项目编号生成
 */
public class ProjectNoUtils {

    // 中文名称转首字母拼音 张三丰 -》 ZSF
    public static String toFirstLetterUpperCase(String chinese) {
        StringBuilder result = new StringBuilder();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        for (char c : chinese.toCharArray()) {
            try {
                // 判断是否是中文字符
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]")) {
                    // 获取拼音首字母
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null && pinyinArray.length > 0) {
                        // 提取拼音首字母并拼接
                        result.append(pinyinArray[0].charAt(0));
                    }
                } else if (Character.isLetter(c)) {
                    // 非中文字母字符，转换为大写并拼接
                    result.append(Character.toUpperCase(c));
                }
//                else {
//                    // 其他非字母字符，直接拼接
//                    result.append(c);
//                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        }
        return result.toString().toUpperCase();
    }

    public static void main(String[] args) {
        String name = "张三丰（哈哈）";
        String result = toFirstLetterUpperCase(name);
        System.out.println(result); // 输出：ZSF
    }
}
